package xyz.xpman.prisonerfitness.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.RequiresApi;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.common.App;
import xyz.xpman.prisonerfitness.kit.TimerKit;
import xyz.xpman.prisonerfitness.service.TestService;


public class GuidePageActivity extends BaseActivity {
    private TestService.Binder binder = null;

    /**
     * 由于安全机制，安卓的UI线程不允许随意被调用。所以只能用Handler来改变UI（目前没用到）。
     */
    final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i("handler", "data: " + msg.getData().getString("data"));
            // tvOut.setText(msg.getData().getString("data"));
        }
    };

    // 绑定服务的方式
    final ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.v("TestService", "onServiceConnected");

            binder = (TestService.Binder) iBinder;

            binder.getService().setCallback(data -> {
                final Bundle b = new Bundle();
                b.putString("data", data);

                final Message msg = new Message();
                msg.setData(b);
                // msg.setTarget(handler);
                // msg.sendToTarget();
                handler.sendMessage(msg);
            });
        }


        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.v("TestService", "onServiceDisconnected");

        }
    };


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        hideStatusBar();
        hideBottomUIMenu();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);

        final Context context = getApplicationContext();
        // 应用上下文（生命周期：app启动 到 app停止）
        App.init(context);



        // 启动服务
        final Intent intent = new Intent(context, TestService.class);
        intent.putExtra("data", "haha!");
        intent.putExtra("data2", "aha!");
//        startService(new Intent(TestService.ACTION));
        startService(intent);
        bindService(intent, conn, BIND_AUTO_CREATE);


        // 页面逻辑
        initData();
        initView();


        TimerKit.DEFAULT.schedule(() -> {
            startActivity(new Intent(this, IndexActivity.class));
            // 查看activity栈：adb shell dumpsys activity | find "包名"
            // 引导页跳转到主页面后直接销毁
            // finish();
        }, 1000);


        TimerKit.DEFAULT.schedule(() -> {
            // 解绑服务
            unbindService(conn);
            // 关闭服务
            stopService(intent);
            finish();
        }, 5000);


//        TimerKit.DEFAULT.schedule(HttpKit::testGet, 3000);
//        TimerKit.DEFAULT.schedule(HttpKit::testPost, 3000);
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    private void initData() {
    }

    private void initView() {
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


}